//
//  WelfareViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/6/22.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "WelfareViewController.h"
#import "CircleProgressView.h"
#import "QuDouJifenController.h"
#import "FlowCurrencyController.h"
#import "WelfareActivityCenterTableViewCell.h"
#import "WelfareExchangeIntegralViewController.h"
#import "WelfareActivityCenterProgressView.h"
#import "WelfareIntegralRecordViewController.h"
#import "IntegralRecordViewController.h"
#import "WelfareH5CollectionViewCell.h"
#import "WelfareH5Model.h"
#import "BigModel.h"
#import "SmallModel.h"
#import "PopupWebView.h"
#import "ExchangeController.h"

#define  PROGREESS_WIDTH jisuan(290) //圆直径
#import <AudioToolbox/AudioToolbox.h>
// 深色
#define Color01ADF3 [UIColor colorWithHexString:@"01ADF3"]
// 浅色
#define Color7CD9FF [UIColor colorWithHexString:@"7CD9FF"]

static NSString *activityCenterCellID = @"activityCenterCellID";
static NSString *fourBtnCellID = @"fourBtnCellID";

@interface WelfareViewController ()<SDCycleScrollViewDelegate, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

{
   SystemSoundID scoreClickBtnID;   // 自己设置的音效 (系统的振动音效 - kSystemSoundID_Vibrate)
    UIWindow *currentWindow;
}

@property (strong, nonatomic) UIScrollView *contentScrollView;

@property (strong, nonatomic) UIView *firstView;
@property (strong, nonatomic) SDCycleScrollView *noticeCycleView;

@property (strong, nonatomic) UIView *activityView;

@property (strong, nonatomic) UIView *fourActivityView;
@property (strong, nonatomic) UICollectionView *fourBtnActivityCollectionView;

@property (strong, nonatomic) SDCycleScrollView *activityActivityScrollView;

// 任务中心（包括titileView和giftProgressView）
@property (strong, nonatomic) UIView *activityCenterView;

@property (strong, nonatomic) NSMutableArray *activityCenterArr;
@property (strong, nonatomic) UIView *giftProgressView;

@property (strong, nonatomic) UITableView *activityTableView;

// 任务中心奖品、进度条view
@property (strong, nonatomic) WelfareActivityCenterProgressView *progressView;

// 任务进度显示 （今日任务完成进度）
@property (strong, nonatomic) UILabel *progressL;

@property (strong, nonatomic) UIButton *gotopBtn;


// 环形进度条
@property (strong, nonatomic) CircleProgressView *circleView;
//趣豆
@property (strong, nonatomic) UILabel *moneyL;
//趣豆字段
@property (strong, nonatomic) UILabel *exchangeAmountL;
// 我的积分字段
@property (strong, nonatomic) UILabel *mineIntegralL;
// 积分
@property (strong, nonatomic) UILabel *integral;
// 已出售趣豆字段
@property (strong, nonatomic) UILabel *exchageL;
// 已出趣豆
@property (strong, nonatomic) UILabel *exchangeMoneyL;

// 头部数据
@property (strong, nonatomic) NSDictionary *headerDataDict;
// h5数据
@property (strong, nonatomic) NSArray *h5DataArr;
// banner数据
@property (strong, nonatomic) NSArray *bannerArr;

@property (strong, nonatomic) UserInfoModel *userInfoModel;

@property (nonatomic, strong) NSMutableArray *bigArray;
@property (nonatomic, strong) NSArray *smallArray;

@property (nonatomic,copy) NSString *webViewUrl;

@property (nonatomic, assign) CGFloat collectionHeight;
@property (nonatomic, assign) CGFloat tableViewHeight;

@property (nonatomic, strong) PopupWebView *popvv;

@property (nonatomic, strong) UIView *lineV;

@end

@implementation WelfareViewController


- (NSMutableArray *)bigArray {
    if (!_bigArray) {
        _bigArray = [[NSMutableArray alloc] init];
    }
    return _bigArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"5a2e388b2886c.wav" ofType:@""]]), &scoreClickBtnID);
    
    self.view.backgroundColor = Colorf2f2f2;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    // 公告数据请求
    [self loadNoticeCycleViewData];
   
    // h5信息请求
    [self loadH5Data];

    [self createUI];
    
    WS(weakSelf);
    NSString *string = [NSString stringWithFormat:@"{\"userid\" : \"%@\"}",[RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey]];
    [PPNetworkHelper POST:@"index/Share/index" parameters:@{@"json":string} success:^(id responseObject) {

        if ([responseObject[@"code"] intValue] == 1) {
            
            weakSelf.webViewUrl = responseObject[@"data"][@"url"];
        }
    } failure:^(NSError *error) {
        
    }];
    
   
}
#pragma mark -banner数据请求
- (void)loadBannerData {
    
    [PPNetworkHelper POST:@"user/Welfare/banner" parameters:nil success:^(id responseObject) {
        
        NSLog(@"福利中心banner%@",responseObject);
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            self.bannerArr = responseObject[@"data"];
            
            NSMutableArray *array = [NSMutableArray array];
            for (NSDictionary *dict in self.bannerArr) {
                
                [array addObject:dict[@"banner"]];
            
            }
            
            self.activityActivityScrollView.imageURLStringsGroup = array;
            
        }else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        }
        
    } failure:^(NSError *error) {
        
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
        }
    }];
    
}

#pragma mark -H5信息请求
- (void)loadH5Data {
    WS(weakSelf);
    [WelfareH5Model welfareFourH5withSuccess:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 1) {
            weakSelf.h5DataArr = responseObject[@"data"];
            [weakSelf.fourBtnActivityCollectionView reloadData];
            NSInteger rows = (weakSelf.h5DataArr.count - 1) / 2 + 1;
            weakSelf.collectionHeight = weakSelf.h5DataArr.count == 0 ? 0 : rows * (jisuan(400) / 2.0 - jisuan(10));
            [weakSelf scroollViewContentSize:weakSelf.collectionHeight TableViewHeight:weakSelf.tableViewHeight];
            weakSelf.lineV.hidden = weakSelf.h5DataArr.count > 2 ? NO : YES;
        }
        
    } fail:^(NSError *error) {
        
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
        }
    }];

}

//更新UI高度
- (void)scroollViewContentSize:(CGFloat)colleCtionHeight TableViewHeight:(CGFloat)tableHeight {
    
    [self.view layoutIfNeeded];
    [self.fourActivityView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(colleCtionHeight);
    }];
    
    self.activityTableView.frame = CGRectMake(0, CGRectGetMaxY(self.fourActivityView.frame)+jisuan(360), WIDTH, tableHeight);
    self.contentScrollView.contentSize  = CGSizeMake(0, tableHeight + jisuan(600) + colleCtionHeight + jisuan(40));
}

#pragma mark -公告数据请求
- (void)loadNoticeCycleViewData {
    
    [PPNetworkHelper POST:@"user/Welfare/exchangelist" parameters:nil success:^(id responseObject) {
        
        NSLog(@"福利中心兑换记录滚动条：%@", responseObject);
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            NSArray *array = responseObject[@"data"];
            
            NSMutableArray *arrM = [NSMutableArray array];
            
            for (NSDictionary *dict in array) {
                
                [arrM addObject:dict[@"title"]];
                
            }
            
            self.noticeCycleView.titlesGroup = arrM.copy;
            
        }else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        
        }
        
    } failure:^(NSError *error) {
        
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
        }
    }];
    
}


- (NSString *)reviseString:(NSString *)str
{
    //直接传入精度丢失有问题的Double类型
    double conversionValue = [str doubleValue];
    NSString *doubleString = [NSString stringWithFormat:@"%lf", conversionValue];
    NSDecimalNumber *decNumber = [NSDecimalNumber decimalNumberWithString:doubleString];
    return [decNumber stringValue];
}

#pragma mark -顶部数据请求
- (void)loadHeaderViewData {
    WS(weakSelf);
    NSString *stringaaaaa = [NSString stringWithFormat:@"{\"user_id\" : \"%@\"}",[RSA encryptString:[CommonUserInfo sharedInstance].user_id?[CommonUserInfo sharedInstance].user_id : @"0" publicKey:PublicKey]];
    [PPNetworkHelper POST:@"welfare/task/index" parameters:@{@"json":stringaaaaa} success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 1) {
            NSArray *array = responseObject[@"data"];
            [weakSelf.bigArray removeAllObjects];
            CGFloat het =   array.count * 30 ;
            for (int i = 0; i < array.count; i ++) {
                BigModel *model = [BigModel mj_objectWithKeyValues:array[i]];
                het = het +model.gheight;
                [self.bigArray addObject:model];
            }
            self.activityTableView.hidden = NO;
            weakSelf.tableViewHeight = het;
            
            [weakSelf scroollViewContentSize:weakSelf.collectionHeight TableViewHeight:weakSelf.tableViewHeight];
            
            [self.activityTableView reloadData];
        }
    } failure:^(NSError *error) {
        
    }];
    
    
    
    NSString *user_id = @"";
    
    if ([CommonUserInfo sharedInstance].user_id == nil) {
        user_id = @"0";
    }else {
        user_id = [CommonUserInfo sharedInstance].user_id;
    }
    
    NSString *par = [NSString stringWithFormat:@"{\"user_id\" : \"%@\"}", [RSA encryptString:user_id publicKey:PublicKey]];

    
    [PPNetworkHelper POST:@"user/Welfare/top" parameters:@{@"json" : par} success:^(id responseObject) {
        
        NSLog(@"福利中心顶部数据请求:%@", responseObject);
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            NSDictionary *dict = responseObject[@"data"];
            self.headerDataDict = [NSDictionary dictionaryWithDictionary:dict];
            
            // user_bean  积分；bean_quota趣豆；top1name趣豆字；top2name我的积分；liulianbgbi已出趣豆；top3name已出售趣豆
            // 进度条进度 =  趣豆 / 积分
            
            NSString *count = nil;
            if ([dict[@"bean_quota"] isKindOfClass:[NSNull class]]) {
                
                count = @"0";
                
            }else {
                
                count = dict[@"bean_quota"];
                
            }
            
            self.moneyL.text = [NSString stringWithFormat:@"%@", count];
            
            self.exchangeAmountL.text = dict[@"top1name"];
            self.mineIntegralL.text = dict[@"top2name"];
            NSString *qudouString = dict[@"user_bean"];
            NSString *userBean = nil;
            if ([qudouString length] <= 0 || [qudouString isKindOfClass:[NSNull class]]) {
                
                userBean = @"0";
            }else {
                
                userBean = dict[@"user_bean"];
            }
            self.integral.text = userBean;
            self.exchageL.text = [NSString stringWithFormat:@"%@",dict[@"top3name"]];
            self.exchangeMoneyL.text = [self reviseString:[NSString stringWithFormat:@"%@", dict[@"liulianbgbi"]]];
            
            [self.circleView setPercent:[userBean floatValue] > [count floatValue] ? [count floatValue] / [userBean floatValue] * 100 : [userBean floatValue] / [count floatValue] *100 animated:YES];
            
        }else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
            
        }
        
    } failure:^(NSError *error) {
        
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
        }
   
    }];
    
}


- (void)gotoTopClick:(UIButton *)btn {
    [self.contentScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

#pragma mark -兑换积分
- (void)exchageClick:(UIButton *)btn {
    
    if (![[CommonAction sharedInstance] isLoginWithParentVc:self]) {
        
        return;
    }
    ExchangeController *vc = [[ExchangeController alloc] init];
    vc.dic = self.headerDataDict;
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark -我的积分
- (void)arrowClick:(UIGestureRecognizer *)recognizer {
    
    if (![[CommonAction sharedInstance] isLoginWithParentVc:self]) {
        return;
    }
//    #import "IntegralRecordViewController.h"
    IntegralRecordViewController *vc = [[IntegralRecordViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -积分可兑换额度
- (void)exchangeClick:(UIGestureRecognizer *)recognizer {
    
    NSLog(@"积分可兑换额度");
    
    if (![[CommonAction sharedInstance] isLoginWithParentVc:self]) {
        
        return;
    }
    
    WelfareIntegralRecordViewController *ctr = [[WelfareIntegralRecordViewController alloc] init];
   
    [self.navigationController pushViewController:ctr animated:YES];
    

}


#pragma mark -cycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    
    if (cycleScrollView == self.activityActivityScrollView) {
        NSDictionary *dict = self.bannerArr[index];
        WebViewController *vc = [[WebViewController alloc] init];
        vc.url = [NSString stringWithFormat:@"%@?user_mobile=%@", dict[@"url"], [CommonUserInfo sharedInstance].phone];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return jisuan(170);
    
}

#pragma mark -delegate \ datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.bigArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    BigModel *model = self.bigArray[section];
    return model.smallArray.count;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = WhiteColor;
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"邀请任务";
    titleLabel.textColor = Color333333;
    titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:font(25)];
    [headerView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(headerView);
    }];
    
    UIImageView *imageLeft = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Leftimage"]];
    UIImageView *imageRight = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Rightimage"]];
    [headerView addSubview:imageLeft];
    [headerView addSubview:imageRight];
    [imageRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_right).offset(5);
        make.centerY.equalTo(titleLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(10, 2));
    }];
    
    [imageLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(titleLabel.mas_left).offset(-5);
        make.centerY.equalTo(titleLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(10, 2));
    }];
    
    BigModel  *model = self.bigArray[section];
    titleLabel.text = model.title;
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BigModel *model = self.bigArray[indexPath.section];
    SmallModel *smallModel = model.smallArray[indexPath.row];
    WelfareActivityCenterTableViewCell *cell = [[WelfareActivityCenterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:activityCenterCellID];
    cell.model = smallModel;
    cell.backgroundColor = WhiteColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (![[CommonAction sharedInstance] isLoginWithParentVc:self]) {

        return;
    }

    self.popvv = [[PopupWebView alloc] init];
    self.popvv.hidden = YES;
    self.popvv.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    [[UIApplication sharedApplication].keyWindow addSubview:self.popvv];
    
    BigModel *model = self.bigArray[indexPath.section];
    SmallModel *smallModel = model.smallArray[indexPath.row];
    WS(weakSelf);
    if ([smallModel.button intValue] == 2) {
         NSString *string = [NSString stringWithFormat:@"{\"user_id\" : \"%@\" ,\"task_id\" : \"%@\"}",[RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey],[RSA encryptString:smallModel.taskId publicKey:PublicKey]];

        [PPNetworkHelper POST:@"welfare/task/task" parameters:@{@"json": string} success:^(id responseObject) {
            if ([responseObject[@"code"] intValue] == 1) {
                [weakSelf loadHeaderViewData];
                weakSelf.popvv.hidden = NO;
                [weakSelf.popvv loadWithHtml:responseObject[@"data"][@"jiangli"]];
                AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
                AudioServicesPlaySystemSound(self->scoreClickBtnID);
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
                    [weakSelf.popvv removeFromSuperview];
                });
            }else {
                [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
            }
            
        } failure:^(NSError *error) {
            [[KYAlertView sharedInstance] showAlertViewWithMessage:@"网络异常，请稍后领取！"];
        }];

    } else {
       
        switch ([smallModel.type intValue]) {
            case 1:
            {
                self.navigationController.tabBarController.selectedIndex = 0;
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
                break;
            case 2:
            {
                self.navigationController.tabBarController.selectedIndex = 2;
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
                break;
            case 3:
            {

            }
                break;
            case 4:
            {
                WebViewController *vc = [[WebViewController alloc] init];
                vc.url = self.webViewUrl;
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;

            default:
                break;
        }
    }
    
}

#pragma mark -collection delegate \ datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.h5DataArr.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WelfareH5CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:fourBtnCellID forIndexPath:indexPath];
    
    cell.H5Model = self.h5DataArr[indexPath.item];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (![[CommonAction sharedInstance] isLoginWithParentVc:self]) {
        return;
    }
    
    WelfareH5Model *model = self.h5DataArr[indexPath.item];
    WebViewController *vc = [[WebViewController alloc] init];
    vc.title = model.title;
    vc.url = [NSString stringWithFormat:@"%@?user_mobile=%@", model.url, [CommonUserInfo sharedInstance].phone];
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)createUI {
    
    self.contentScrollView.frame = CGRectMake(0, 0, WIDTH, HEIGHT-Height_TabBar-Height_NavBar);
    [self.view addSubview:self.contentScrollView];
    
//    [self createLimitView];
    [self createActivityView];
//    [self createFourBtnView];
    [self createActivityCenterView];
 
}


- (void)createActivityCenterView {
    
    __weak typeof(self) weakSelf = self;
    
    UIView *activityCenterView = [[UIView alloc] init];
    self.activityCenterView = activityCenterView;
    activityCenterView.backgroundColor = WhiteColor;
    [self.contentScrollView addSubview:activityCenterView];
    [activityCenterView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.activityView.mas_bottom).with.offset(jisuan(20));
        make.height.mas_equalTo(jisuan(120));
    }];
    
    [self createActivityCenterContentView];
    
    [self.contentScrollView addSubview:self.activityTableView];
    
    [activityCenterView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.activityView.mas_bottom).with.offset(jisuan(20));
        make.height.mas_equalTo(jisuan(120));
    }];

  
}

- (void)createActivityCenterContentView {
    
    __weak typeof(self) weakSelf = self;

    UIView *titleView = [[UIView alloc] init];
    [self.activityCenterView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.activityCenterView.mas_left);
        make.right.mas_equalTo(weakSelf.activityCenterView.mas_right);
        make.top.mas_equalTo(weakSelf.activityCenterView.mas_top);
        make.height.mas_equalTo(jisuan(140));
    }];
    
    UILabel *titleL = [[UILabel alloc] init];
    titleL.text = @"任务中心";
    titleL.textColor = Color333333;
    titleL.textAlignment = NSTextAlignmentCenter;
    titleL.font = [UIFont fontWithName:@"Helvetica-Bold" size:font(30)];
    [titleView addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(titleView.mas_centerX);
        make.top.mas_equalTo(titleView.mas_top).with.offset(jisuan(28));
    }];
    
    UIImageView *leftIV = [[UIImageView alloc] init];
    leftIV.contentMode = UIViewContentModeScaleAspectFit;
    leftIV.image = [UIImage imageNamed:@"sectionHeaderLeft"];
    [titleView addSubview:leftIV];
    [leftIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(titleL.mas_centerY);
        make.right.mas_equalTo(titleL.mas_left).with.offset(-jisuan(10));
        make.width.mas_equalTo(jisuan(30));
        make.height.mas_equalTo(jisuan(24));
    }];
    UIImageView *rightIV = [[UIImageView alloc] init];
    rightIV.contentMode = UIViewContentModeScaleAspectFit;
    rightIV.image = [UIImage imageNamed:@"sectionHeaderRight"];
    [titleView addSubview:rightIV];
    [rightIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(titleL.mas_centerY);
        make.left.mas_equalTo(titleL.mas_right).with.offset(jisuan(10));
        make.width.mas_equalTo(jisuan(30));
        make.height.mas_equalTo(jisuan(24));
    }];
    
    UILabel *desL = [[UILabel alloc] init];
    desL.textAlignment = NSTextAlignmentCenter;
    desL.textColor = Color999999;
    desL.font = Font(20);
    desL.text = @"把你购物的钱赚回来";
    [titleView addSubview:desL];
    [desL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(titleL.mas_bottom).with.offset(jisuan(16));
        make.centerX.mas_equalTo(titleView.mas_centerX);
   
    }];

}

- (void)createFourBtnView {
    
    __weak typeof(self) weakSelf = self;
    UIView *fourActivityView = [[UIView alloc] init];
    self.fourActivityView = fourActivityView;
    fourActivityView.layer.cornerRadius = jisuan(10);
    fourActivityView.layer.masksToBounds = YES;
    fourActivityView.backgroundColor = WhiteColor;
    [self.contentScrollView addSubview:fourActivityView];
    [fourActivityView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(20));
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(20));
        make.top.mas_equalTo(weakSelf.activityView.mas_bottom).with.offset(jisuan(20));
    }];
    
    [self.fourActivityView addSubview:self.fourBtnActivityCollectionView];
    [self.fourBtnActivityCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.fourActivityView.mas_left);
        make.right.mas_equalTo(weakSelf.fourActivityView.mas_right);
        make.top.mas_equalTo(weakSelf.fourActivityView.mas_top);
        make.bottom.mas_equalTo(weakSelf.fourActivityView.mas_bottom);
        
    }];
    
    UIView *lineV = [[UIView alloc] init];
    lineV.backgroundColor = Colordddddd;
    [fourActivityView addSubview:lineV];
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(fourActivityView.mas_left);
        make.right.mas_equalTo(fourActivityView.mas_right);
        make.centerY.mas_equalTo(fourActivityView.mas_centerY);
        make.height.mas_equalTo(jisuan(2));
    }];
    self.lineV = lineV;
    
}

- (void)createActivityView {
    
    __weak typeof(self) weakSelf = self;
    
    UIView *activityView = [[UIView alloc] init];
    self.activityView = activityView;
    activityView.backgroundColor = WhiteColor;
    [self.contentScrollView addSubview:activityView];
    [activityView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.contentScrollView.mas_top).with.offset(jisuan(20));
//        make.height.mas_equalTo(jisuan(260));
        make.height.mas_equalTo(jisuan(200));
        
    }];

    SDCycleScrollView *activityActivityScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero imageURLStringsGroup:nil];
    self.activityActivityScrollView = activityActivityScrollView;
    self.activityActivityScrollView.backgroundColor = WhiteColor;

    self.activityActivityScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFit;
 
    self.activityActivityScrollView.autoScroll = NO;
    self.activityActivityScrollView.delegate = self;

    self.activityActivityScrollView.showPageControl = YES;
    self.activityActivityScrollView.pageControlDotSize = CGSizeMake(jisuan(11), jisuan(11));
    self.activityActivityScrollView.currentPageDotImage = [UIImage imageNamed:@"currentPageDotImage2"];
    self.activityActivityScrollView.pageDotImage = [UIImage imageNamed:@"pageDotImage2"];
    self.activityActivityScrollView.pageControlBottomOffset = -jisuan(15);
    [activityView addSubview:activityActivityScrollView];
    [activityActivityScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(activityView.mas_left);
        make.right.mas_equalTo(activityView.mas_right);
        make.top.mas_equalTo(activityView.mas_top).with.offset(jisuan(0));
        make.bottom.mas_equalTo(activityView.mas_bottom).with.offset(-jisuan(0));
        
    }];
    
}

- (void)createLimitView {
    
//    UIView *firstView = [[UIView alloc] init];
//    self.firstView = firstView;
//    firstView.backgroundColor = WhiteColor;
//    [self.contentScrollView addSubview:firstView];
    
//    __weak typeof(self) weakSelf = self;
//    [firstView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.mas_equalTo(weakSelf.view.mas_left);
//        make.right.mas_equalTo(weakSelf.view.mas_right);
//        make.top.mas_equalTo(weakSelf.contentScrollView.mas_top);
//        make.height.mas_equalTo(jisuan(610));
//    }];
//
//    UIImageView *firstBgView = [[UIImageView alloc] init];
//    firstBgView.backgroundColor = WhiteColor;
//    firstBgView.layer.cornerRadius = jisuan(10);
//    firstBgView.layer.shadowColor = Colorb6b6b6.CGColor;
//    firstBgView.layer.shadowOffset = CGSizeMake(0, 0);
//    firstBgView.layer.shadowOpacity = 0.5;
//    firstBgView.layer.shadowRadius = jisuan(5);
//    [firstView addSubview:firstBgView];
//    [firstBgView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.centerX.mas_equalTo(firstView.mas_centerX);
//        //        make.centerY.mas_equalTo(firstView.mas_centerY);
//        make.top.mas_equalTo(firstView.mas_top).with.offset(jisuan(10));
//
//        make.width.mas_equalTo(jisuan(710));
//        make.height.mas_equalTo(jisuan(500));
//    }];
//
//    CGFloat circleX = (WIDTH - PROGREESS_WIDTH) / 2.0;
//    CircleProgressView *circleView = [[CircleProgressView alloc] initWithFrame:CGRectMake(circleX, jisuan(45), PROGREESS_WIDTH, PROGREESS_WIDTH)];
//    [firstView addSubview:circleView];
//    self.circleView = circleView;
//
//
//    UILabel *moneyL = [[UILabel alloc] init];
//    self.moneyL = moneyL;
//    moneyL.textColor = Color1f89ff;
//    [moneyL sizeToFit];
//    moneyL.textAlignment = NSTextAlignmentCenter;
//    moneyL.font = [UIFont fontWithName:@"DustHome" size:font(48)];
//    moneyL.text = @"0.00";
//    [firstView addSubview:moneyL];
//    [moneyL mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.centerX.mas_equalTo(firstView.centerX);
//        make.top.mas_equalTo(firstView.mas_top).with.offset(jisuan(150));
//    }];
//
//    UILabel *exchangeAmountL = [[UILabel alloc] init];
//    self.exchangeAmountL = exchangeAmountL;
//    exchangeAmountL.textAlignment = NSTextAlignmentCenter;
//    exchangeAmountL.textColor = Color1f89ff;
//    exchangeAmountL.font = Font(20);
//    [firstView addSubview:exchangeAmountL];
//    [exchangeAmountL mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.centerX.mas_equalTo(firstView.mas_centerX);
//        make.top.mas_equalTo(moneyL.mas_bottom).with.offset(jisuan(10));
//    }];
//
//    UIButton *exchangeBtn = [[UIButton alloc] init];
//    [exchangeBtn setImage:[UIImage imageNamed:@"welfareExchangeBtn"] forState:UIControlStateNormal];
//    [exchangeBtn setImage:[UIImage imageNamed:@"welfareExchangeBtn"] forState:UIControlStateSelected];
//    [exchangeBtn addTarget:self action:@selector(exchageClick:) forControlEvents:UIControlEventTouchUpInside];
//    [firstView addSubview:exchangeBtn];
//    [exchangeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.centerX.mas_equalTo(firstView.mas_centerX);
//        make.top.mas_equalTo(exchangeAmountL.mas_bottom).with.offset(jisuan(30));
//        make.width.mas_equalTo(jisuan(160));
//        make.height.mas_equalTo(jisuan(40));
//    }];
//
//    UIView *integralView = [[UIView alloc] init];
//    integralView.userInteractionEnabled = YES;
//    UITapGestureRecognizer *arrowGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(arrowClick:)];
//    [integralView addGestureRecognizer:arrowGesture];
//    [firstView addSubview:integralView];
//    [integralView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.mas_equalTo(firstBgView.mas_left);
//        make.bottom.mas_equalTo(firstBgView.mas_bottom);
//        make.width.mas_equalTo(jisuan(710) / 2.0);
//        make.height.mas_equalTo(jisuan(150));
//
//    }];
//
//    UIImageView *integralIV = [[UIImageView alloc] init];
//    integralIV.contentMode = UIViewContentModeScaleAspectFit;
//    integralIV.image = [UIImage imageNamed:@"welfareIntegralLogo"];
//    [integralView addSubview:integralIV];
//    [integralIV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(integralView.mas_left).with.offset(jisuan(70));
//        make.centerY.mas_equalTo(integralView.mas_centerY);
//        make.width.mas_equalTo(jisuan(30));
//        make.height.mas_equalTo(jisuan(30));
//    }];
//
//    UILabel *mineIntegralL = [[UILabel alloc] init];
//    self.mineIntegralL = mineIntegralL;
//    mineIntegralL.textAlignment = NSTextAlignmentLeft;
//    mineIntegralL.textColor = Color333333;
//    mineIntegralL.font = Font(24);
//    [integralView addSubview:mineIntegralL];
//    [mineIntegralL mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.mas_equalTo(integralIV.mas_right).with.offset(jisuan(30));
//        make.bottom.mas_equalTo(integralView.mas_bottom).with.offset(-jisuan(40));
//
//    }];
//
//    UIImageView *arrow = [[UIImageView alloc] init];
//    arrow.contentMode = UIViewContentModeScaleAspectFit;
//    arrow.image = [UIImage imageNamed:@"welfareArrowbg"];
//    [integralView addSubview:arrow];
//    arrow.userInteractionEnabled = YES;
//    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.right.mas_equalTo(integralView.mas_right).with.offset(-jisuan(44));
//        make.centerY.mas_equalTo(integralView.mas_centerY);
//        make.width.mas_equalTo(jisuan(11));
//        make.height.mas_equalTo(jisuan(20));
//    }];
//
//    UILabel *integral = [[UILabel alloc] init];
//    self.integral = integral;
//    integral.textAlignment = NSTextAlignmentCenter;
//    integral.textColor = Colorff0000;
//    integral.font = [UIFont fontWithName:@"DustHome" size:font(28)];
//    integral.numberOfLines = 0;
//    [integralView addSubview:integral];
//    [integral mas_makeConstraints:^(MASConstraintMaker *make) {
//
//
//        make.centerX.mas_equalTo(mineIntegralL.mas_centerX);
//        make.bottom.mas_equalTo(mineIntegralL.mas_top).with.offset(-jisuan(16));
//        make.width.mas_equalTo(jisuan(160));
//
//    }];
//
//    UIView *exchageView = [[UIView alloc] init];
//    UITapGestureRecognizer *exchageGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(exchangeClick:)];
//    [exchageView addGestureRecognizer:exchageGesture];
//
//    [firstView addSubview:exchageView];
//    [exchageView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.right.mas_equalTo(firstBgView.mas_right);
//        make.bottom.mas_equalTo(firstBgView.mas_bottom);
//        make.width.mas_equalTo(jisuan(710) / 2.0);
//        make.height.mas_equalTo(jisuan(150));
//    }];
//
//    UILabel *exchageL = [[UILabel alloc] init];
//    self.exchageL = exchageL;
//    exchageL.textAlignment = NSTextAlignmentRight;
//    exchageL.textColor = Color333333;
//    exchageL.font = Font(24);
//    [exchageView addSubview:exchageL];
//    [exchageL mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.right.mas_equalTo(exchageView.mas_right).with.offset(-jisuan(70));
//        make.bottom.mas_equalTo(exchageView.mas_bottom).with.offset(-jisuan(40));
//
//    }];
//
//    UIImageView *exchangeIV = [[UIImageView alloc] init];
//    exchangeIV.contentMode = UIViewContentModeScaleAspectFit;
//    exchangeIV.image = [UIImage imageNamed:@"welfareExchageLogoBg"];
//    [exchageView addSubview:exchangeIV];
//    [exchangeIV mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.right.mas_equalTo(exchageL.mas_left).with.offset(-jisuan(40));
//        make.centerY.mas_equalTo(exchageView.mas_centerY);
//        make.width.mas_equalTo(jisuan(30));
//        make.height.mas_equalTo(jisuan(30));
//    }];
//
//    UILabel *exchangeMoneyL = [[UILabel alloc] init];
//    self.exchangeMoneyL = exchangeMoneyL;
//    exchangeMoneyL.textAlignment = NSTextAlignmentCenter;
//    exchangeMoneyL.textColor = Colorff0000;
//    exchangeMoneyL.font = [UIFont fontWithName:@"DustHome" size:font(28)];
//    exchangeMoneyL.numberOfLines = 0;
//    [exchageView addSubview:exchangeMoneyL];
//    [exchangeMoneyL mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.centerX.mas_equalTo(exchageL.mas_centerX);
//        make.bottom.mas_equalTo(exchageL.mas_top).with.offset(-jisuan(16));
//        make.width.mas_equalTo(jisuan(168));
//
//    }];
//
//    UIView *lineView = [[UIView alloc] init];
//    lineView.backgroundColor = Colordddddd;
//    [firstView addSubview:lineView];
//    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.mas_equalTo(firstView.mas_left);
//        make.right.mas_equalTo(firstView.mas_right);
//        make.bottom.mas_equalTo(firstView.mas_bottom).with.offset(-jisuan(70));
//        make.height.mas_equalTo(jisuan(2));
//    }];
//
//    UIView *noticeView = [[UIView alloc] init];
//    [firstView addSubview:noticeView];
//    [noticeView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.mas_equalTo(firstView.mas_left);
//        make.right.mas_equalTo(firstView.mas_right);
//        make.bottom.mas_equalTo(firstView.mas_bottom);
//        make.top.mas_equalTo(lineView.mas_bottom);
//    }];
//
//    UIImageView *noticeIV = [[UIImageView alloc] init];
//    noticeIV.contentMode = UIViewContentModeScaleAspectFit;
//    noticeIV.image = [UIImage imageNamed:@"welfareNoticeBG"];
//    [noticeView addSubview:noticeIV];
//    [noticeIV mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.mas_equalTo(noticeView.mas_left).with.offset(jisuan(20));
//        make.centerY.mas_equalTo(noticeView.mas_centerY);
//        make.width.mas_equalTo(jisuan(25));
//        make.height.mas_equalTo(jisuan(30));
//    }];
//
//    SDCycleScrollView *noticeCycleView = [[SDCycleScrollView alloc] init];
//    self.noticeCycleView = noticeCycleView;
//    noticeCycleView.backgroundColor = WhiteColor;
//    noticeCycleView.delegate = self;
//    noticeCycleView.onlyDisplayText = YES;
//    noticeCycleView.scrollDirection = UICollectionViewScrollDirectionVertical;
//    noticeCycleView.titleLabelTextColor = Color333333;
//    noticeCycleView.titleLabelBackgroundColor = WhiteColor;
//    noticeCycleView.titleLabelTextAlignment = NSTextAlignmentLeft;
//    noticeCycleView.titleLabelTextFont = Font(20);
//    [noticeCycleView disableScrollGesture];
//    [noticeView addSubview:noticeCycleView];
//    [noticeCycleView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.mas_equalTo(noticeIV.mas_right).with.offset(jisuan(20));
//        make.right.mas_equalTo(noticeView.mas_right).with.offset(-jisuan(20));
//        make.centerY.mas_equalTo(noticeView.mas_centerY);
//        make.height.mas_equalTo(jisuan(60));
//    }];
    
}

#pragma mark -lazy
- (UIScrollView *)contentScrollView {
    
    if (!_contentScrollView) {
        
        _contentScrollView = [[UIScrollView alloc] init];
        _contentScrollView.backgroundColor = Colorf2f2f2;
        _contentScrollView.showsVerticalScrollIndicator = NO;
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        
        _contentScrollView.delegate = self;
        
        if (@available(iOS 11.0, *)) {
            
            _contentScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
    }
    
    return _contentScrollView;
}

- (NSMutableArray *)activityCenterArr {
    
    if (!_activityCenterArr) {
        
        _activityCenterArr = [NSMutableArray array];
    }

    return _activityCenterArr;
}

- (UITableView *)activityTableView {
    
    if (!_activityTableView) {
        
        _activityTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        
        _activityTableView.backgroundColor = WhiteColor;
        
        _activityTableView.delegate = self;
        _activityTableView.dataSource = self;
        
        _activityTableView.showsVerticalScrollIndicator = NO;
        _activityTableView.showsHorizontalScrollIndicator = NO;
        _activityTableView.scrollEnabled = NO;
        _activityTableView.estimatedRowHeight = 0;
        _activityTableView.estimatedSectionHeaderHeight = 0;
        _activityTableView.estimatedSectionFooterHeight = 0;
        
        _activityTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_activityTableView registerClass:[WelfareActivityCenterTableViewCell class] forCellReuseIdentifier:activityCenterCellID];
        
        if (@available(iOS 11.0, *)) {
            
            _activityTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        }
        
    }
    
    return _activityTableView;
    
}

- (UICollectionView *)fourBtnActivityCollectionView {
    
    if (!_fourBtnActivityCollectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        CGFloat width = (WIDTH - jisuan(20) * 2) / 2.0 - jisuan(10);
        CGFloat height = jisuan(400) / 2.0 - jisuan(10);
        flowLayout.itemSize = CGSizeMake(width, height);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumLineSpacing = jisuan(5);
        flowLayout.minimumInteritemSpacing = jisuan(5);

        _fourBtnActivityCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _fourBtnActivityCollectionView.backgroundColor = WhiteColor;
        _fourBtnActivityCollectionView.scrollEnabled = NO;
        _fourBtnActivityCollectionView.bounces = NO;
        
        _fourBtnActivityCollectionView.delegate = self;
        _fourBtnActivityCollectionView.dataSource = self;
        
        _fourBtnActivityCollectionView.showsVerticalScrollIndicator = NO;
        _fourBtnActivityCollectionView.showsHorizontalScrollIndicator = NO;
        
        [_fourBtnActivityCollectionView registerClass:[WelfareH5CollectionViewCell class] forCellWithReuseIdentifier:fourBtnCellID];
        
        if (@available(iOS 11.0, *)) {
            _fourBtnActivityCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        }
   
    }
    
    return _fourBtnActivityCollectionView;
    
}

#pragma mark -设置
- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
    [self loadBannerData];
    __weak typeof(self) weakSelf = self;
    [self loadHeaderViewData];
    if ([CommonUserInfo sharedInstance].isLogin) {
        [[CommonAction sharedInstance] userVisitRecordWithRoute:@"福利"];
        [[CommonAction sharedInstance] getUserInfoWithBlock:^(id result) {
            weakSelf.userInfoModel = result;
        }];
    }else {
        self.userInfoModel = nil;
    }
    [TalkingData trackPageBegin:@"福利页"];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [TalkingData trackPageEnd:@"福利页"];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
